package com.wj.gulimall.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rabbitmq.client.Channel;
import com.wj.common.utils.PageUtils;
import com.wj.common.utils.Query;
import com.wj.gulimall.order.dao.OrderItemDao;
import com.wj.gulimall.order.entity.OrderEntity;
import com.wj.gulimall.order.entity.OrderItemEntity;
import com.wj.gulimall.order.entity.OrderReturnReasonEntity;
import com.wj.gulimall.order.service.OrderItemService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

@RabbitListener(queues = {"hello-java.queue"})
@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    /**
     * 接收消息
     *
     * @param message
     * @param returnReasonEntity
     * @param channel
     */
    //@RabbitListener(queues = {"hello-java.queue"})
    @RabbitHandler
    public void recieveMessage(Message message, OrderReturnReasonEntity returnReasonEntity, Channel channel) {
        System.out.println("接收到消息");
        System.out.println(message);
        System.out.println(returnReasonEntity);

        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.println(deliveryTag);
        try {
            if (deliveryTag % 2 == 0) {
                //ack
                channel.basicAck(deliveryTag, false);
            } else {
                //nack
                channel.basicNack(deliveryTag, false, false);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RabbitHandler
    public void recieveMessage(Message message, OrderEntity orderEntity, Channel channel) throws InterruptedException {
        System.out.println("接收到消息");
        System.out.println(message);
        System.out.println(orderEntity);
    }
}